package com.xcc.mota.sprite;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.xcc.mota.event.TouchEvent;
import com.xcc.mota.utils.Logger;

/**
 * 精灵父类
 * Created by Administrator on 2018/1/8.
 */
public abstract class BaseSprite {
    protected int x, y;//这是中心点位置
    private RectF rectF;//所占范围，需要绘制的时候自己计算
    private int down;
    private OnClickListener onClickListener;

    public void setX(int x) {
        this.x = x;
        rectF = null;
    }

    public void setY(int y) {
        this.y = y;
        rectF = null;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public RectF getRectF() {
        return rectF;
    }

    public void setRectF(RectF rectF) {
        this.rectF = rectF;
        if (Logger.isDebug && rectF != null) {
            Logger.out("-------精灵位置-------");
            Logger.out("-left-" + rectF.left + "-right-" + rectF.right);
            Logger.out("-top-" + rectF.top + "-bottom-" + rectF.bottom);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public abstract void onDraw(Canvas canvas, Paint paint);

    public void onTouchEvent(TouchEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                down = 1;
                if (Logger.isDebug) Logger.out("=======触发按下事件=======");
                break;
            case MotionEvent.ACTION_UP:
                if (Logger.isDebug) Logger.out("=======触发抬起事件=======");
                if (down == 1) {
                    if (Logger.isDebug) Logger.out("=======触发点击事件=======");
                    if (onClickListener != null) onClickListener.onClick(this);
                }
            default:
                down = 0;
                break;
        }
    }

    public interface OnClickListener {
        void onClick(BaseSprite sprite);
    }
}
